package com.my.ads.lib.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.myadlib.R;
import com.my.ads.lib.AdSummary;
import com.my.ads.lib.Config;

/**
 * Created by xuanyang on 2018/3/16.
 */

public class PhoneStateActivity extends BaseActivity {

    private ImageView img_back;
    private ImageView imgAd;
    private TextView text_title;
//    private RelativeLayout relative_ad_1;
    private RelativeLayout relative_ad_2;
    private ScrollView scrollView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_state);
//        Log.e("ju","xe");
        initViews();
        start();

    }

    private void start() {

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 页面结束怎么处理
                finish();
            }
        });

        imgAd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgAd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdSummary.getInstance().showInterstitialAd(PhoneStateActivity.this, "");
                        Intent in = new Intent(PhoneStateActivity.this, GiftActivity.class);
                        startActivity(in);
                    }
                });
            }
        });

        if ("clean".equals(getIntent().getStringExtra("type"))) {
            text_title.setText(getResources().getString(R.string.garbage_cleaning));
            scrollView.setBackgroundColor(Color.parseColor("#0c84ff"));
        } else if ("accelerate".equals(getIntent().getStringExtra("type"))) {
            text_title.setText(getResources().getString(R.string.memory_acceleration));
            scrollView.setBackgroundColor(Color.parseColor("#048cef"));
        }

        showAd();
    }

    private void showAd() {
        try {
//            AdSummary.getInstance().showBannerAd(relative_ad_1);
            AdSummary.getInstance().showNativeAd(this,relative_ad_2, false);
        } catch (Exception e) {

        }
    }

    private void initViews() {
        img_back = (ImageView) findViewById(R.id.img_back);
        text_title = (TextView) findViewById(R.id.text_title);
//        relative_ad_1 = (RelativeLayout) findViewById(R.id.relative_ad_1);
        relative_ad_2 = (RelativeLayout) findViewById(R.id.relative_ad_2);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        imgAd = (ImageView) findViewById(R.id.img_ad);
    }

    @Override
    public void onResume() {
        super.onResume();
        resume();
    }

    private void resume() {
        SharedPreferences sp_1 = getSharedPreferences(Config.NativeAdTarget, Context.MODE_PRIVATE);
        Long show_time = sp_1.getLong(Config.NativeAdTimeShow, 0L);
        // 刷新原生广告
        if (show_time != 0L && ((System.currentTimeMillis() - show_time) > Config.UpdateNativeAd * 1000)) {
            try {
                AdSummary.getInstance().showNativeAd(this,relative_ad_2,false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


//        SharedPreferences sp_2 = getSharedPreferences(Config.BannerAdTarget, Context.MODE_PRIVATE);
//        Long show_time_2 = sp_2.getLong(Config.BannerAdTimeShow, 0L);
//        // 刷新banner广告
//        if (show_time_2 != 0L && ((System.currentTimeMillis() - show_time_2) > Config.UpdateNativeAd * 1000)) {
//            AdSummary.getInstance().showBannerAd(relative_ad_1);
//        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
